import operator
from functools import reduce

import six
from django.db import models
from django.db.models import Q
from django.db.models.constants import LOOKUP_SEP
from django_filters import utils, CharFilter
from django_filters.rest_framework import backends

from super.utils.rest import filterset


class DjangoFilterBackend(backends.DjangoFilterBackend):
    filterset_base = filterset.FilterSet
    # lookup_prefixes = {
    #     '^': 'istartswith',
    #     '=': 'iexact',
    #     '@': 'search',
    #     '$': 'iregex',
    #     '~': 'icontains'
    # }
    #
    # def construct_search(self, field_name):
    #     lookup = self.lookup_prefixes.get(field_name[0])
    #     if lookup:
    #         field_name = field_name[1:]
    #     else:
    #         lookup = 'icontains'
    #     return LOOKUP_SEP.join([field_name, lookup])
    #
    # def find_filter_lookups(self, orm_lookups, search_term_key):
    #     for lookup in orm_lookups:
    #         if lookup.find(search_term_key) >= 0:
    #             return lookup
    #     return None
    #
    # def filter_queryset(self, request, queryset, view):
    #     _filterset = self.get_filterset(request, queryset, view)
    #     if _filterset is None:
    #         return queryset
    #     if _filterset.__class__.__name__ == 'AutoFilterSet':
    #         queryset = _filterset.queryset
    #         conditions = []
    #         queries = self.filter_field(_filterset)
    #         if len(queries) > 0:
    #             conditions.append(reduce(operator.and_, queries))
    #             queryset = queryset.filter(reduce(operator.and_, conditions))
    #             return queryset
    #         else:
    #             return queryset
    #
    #     if not _filterset.is_valid() and self.raise_exception:
    #         raise utils.translate_validation(_filterset.errors)
    #     return _filterset.qs
    #
    # def find_lookups(self, _filterset):
    #     lookups = []
    #     for search_field in _filterset.filters:
    #         if isinstance(_filterset.filters[search_field], CharFilter):
    #             lookups.append(self.construct_search(six.text_type(search_field)))
    #         else:
    #             lookups.append(search_field)
    #     return lookups
    #
    # def filter_field(self, _filterset):
    #     """
    #     自定义字段查询
    #     """
    #     lookups = self.find_lookups(_filterset)
    #     queries = []
    #     for field_name in _filterset.data.keys():
    #         field = _filterset.queryset.model._meta.get_field(field_name)
    #         if isinstance(field, models.JSONField):
    #             values = _filterset.data.getlist(field_name)
    #             queries += self.filter_json_field(field, values)
    #         else:
    #             self.filter_normal_field(_filterset, lookups, field_name)
    #     return queries
    #
    # def filter_normal_field(self, _filterset, lookups, field_name):
    #     queries = []
    #     orm_lookup = self.find_filter_lookups(lookups, field_name)
    #     if _filterset.data[field_name] and orm_lookup:
    #         query = Q(**{orm_lookup: _filterset.data[field_name]})
    #         queries.append(query)
    #     return queries
